package com.happo.bustwit;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

public class NotesActivity extends Activity {

	private static final String tag = NotesActivity.class.getSimpleName();

	SharedPreferences preferences;
	Typeface typeFace;
	EditText editText;
	FrameLayout notesFrameLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notes);
		editText = (EditText) findViewById(R.id.noteText);
		notesFrameLayout = (FrameLayout) findViewById(R.id.notesFrameLayout);
	    try {
			ActionBar actionBar = getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);
		} catch (Exception e) {
			Log.w(tag, e.toString());
		}
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		String font = preferences.getString("font", "");
		if (!font.equalsIgnoreCase("")) {
			typeFace = Typeface.createFromAsset(getAssets(), font);
		} else {
			typeFace = Typeface.DEFAULT;
		}
		editText.setTypeface(typeFace);
		editText.setText(preferences.getString("saved_note", getString(R.string.pangram)));
		float add = Float.parseFloat(preferences.getString("line_spacing_extra", "0"));
		float mult = Float.parseFloat(preferences.getString("line_spacing_multiplier", "1"));
		boolean enableLineSeparator = preferences.getBoolean("enable_line_separator", true);
		if (!enableLineSeparator) {
			notesFrameLayout.setBackgroundDrawable(null);
		}
		editText.setLineSpacing(add, mult);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Editor editor = preferences.edit();
    	editor.putString("saved_note", editText.getText().toString());
    	editor.commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	            Intent intent = new Intent(this, BusTwitActivity.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
